package com.woniuxy.kaer.user.controller;


import com.woniuxy.kaer.commons.jwtUtil.JwtUtil;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.user.model.UserLog;
import com.woniuxy.kaer.user.service.UserLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sun
 * @since 2025年02月17日
 */
@RestController
@RequestMapping("/userLog")
@Api(tags = "用户登录日志")
public class UserLogController {
    @Resource
    private UserLogService userLogService;
    @Resource
    private JwtUtil jwtUtil;
    @ApiOperation(value = "添加用户登录日志")
    @PostMapping("/addUserLog")
    public Result addUserLog(@RequestHeader("token") String token, HttpServletRequest request)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        userLogService.addUserLog(userId, request);  // 传递 HttpServletRequest
        return Result.succeed("添加用户登录日志成功", null);
    }

    //查询用户登录日志
    @ApiOperation(value = "查询用户登录日志")
    @PostMapping("/getUserLog")
    public Result getUserLog(@RequestHeader("token") String token)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        UserLog userLog =userLogService.getUserLog(userId);
        return Result.succeed("查询登录日志成功",null);
    }

}

